/*
 * Created on Jul 19, 2005
 */
package persistence;

/**
 * @author Valter Camargo
 *
 */

import java.sql.*;
import java.util.Vector; 


public class ColumnHandler {
	
	
	   public Vector returnColNames(String table) throws SQLException, ClassNotFoundException {

	    Vector columns = new Vector();

	    ResultSet rs = TableManager.findallDB(table);

	    for (int i=1; i < rs.getMetaData().getColumnCount() || i == rs.getMetaData().getColumnCount(); i++){
	          //System.out.println ("column name : " + rs.getMetaData().getColumnName(i));
	          columns.addElement(rs.getMetaData().getColumnName(i));
	    }

	    return columns;

	   }
	   
	   public void assignColValues(PersistentRoot pr){

	    //System.out.println (" --- Starting assignColValues(Pr). The type class is : " + pr.getClass().getName());

	    try
	    {
	      Vector getMethods = new Vector();
	      Vector retornos = new Vector();
	      Vector resultados = new Vector();

	      getMethods = pr.getGetMethods();

	      retornos = pr.getReturnIndex();

	      //System.out.println ("getMethods obtained : " + getMethods);
	      //System.out.println ("Returns : " + retornos);
	      
	      pr.setColValues(colValuesEmpty(pr));
	      pr.setColValues(getMethods, retornos);

	    }

	    catch(ClassCastException e){
	      System.out.println ("CATCH: ColumnHandler (ClassCastException - msg: " + e.getMessage());

	    }
	  
	   }
	   
	   public void assignColValues(PersistentRoot pr, Object[] args){

	    //System.out.println (" --- Starting assignColValues(Pr, args[]). The type class is : " + pr.getClass().getName());

	    try
	    {
	      Vector getMethods = new Vector();
	      Vector retornos = new Vector();
	      Vector resultados = new Vector();

	      getMethods = pr.getGetMethods();

	      retornos = pr.getReturnIndex();

	      //System.out.println ("getMethods obtained : " + getMethods);
	      //System.out.println ("Returns : " + retornos);
	      
	      pr.setColValues(getMethods, retornos);

	    }

	    catch(ClassCastException e){
	      System.out.println ("CATCH: ColumnHandler (ClassCastException - msg: " + e.getMessage());

	    }
	   
	   }
	   
	   public Vector colValuesEmpty(PersistentRoot pr){
	    Vector v = new Vector();
	    Vector v1 = new Vector();
	    v = pr.getColNames();
	    for (int i = 1; i <= v.size(); i++){
	      v1.addElement("0");
	    }
	    return v1;

	  }

}
